//
//  MEnjoyDetailViewController.m
//  NeusoftHBSJSober
//
//  Created by Apple-21 on 15/11/11.
//  Copyright (c) 2015年 com.neusoftsober. All rights reserved.
//

#import "MEnjoyDetailViewController.h"
#import "MTicketService.h"
#import "MSalemanModel.h"
@interface MEnjoyDetailViewController ()

- (IBAction)backAction:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UILabel *salemanNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *salemanAddressLabel;
@property (weak, nonatomic) IBOutlet UILabel *salemanPhoneLabel;
@property (weak, nonatomic) IBOutlet UIButton *merchantDetailBtn;
- (IBAction)merchantDetailAction:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UIButton *specailBtn;
- (IBAction)specailAction:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UILabel *feeLabel;
@property (weak, nonatomic) IBOutlet UILabel *originalCashLabel;
@property (weak, nonatomic) IBOutlet UILabel *currentCashLabel;
@property (weak, nonatomic) IBOutlet UILabel *friendCountLabel;
@property (weak, nonatomic) IBOutlet UILabel *rateLabel;
@property (weak, nonatomic) IBOutlet UIView *moneyView;
@property (weak, nonatomic) IBOutlet UILabel *noticeLabel;
@property (weak, nonatomic) IBOutlet UIProgressView *progressBar;
@property (weak, nonatomic) IBOutlet UILabel *fullLevelLabel;
@property (weak, nonatomic) IBOutlet UILabel *nowPriceLabel;


@end

@implementation MEnjoyDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self _initButton];
}
//  异步加载
- (void)viewWillAppear:(BOOL)animated {

    [self _loadFeeCash];
    [self _loadSalemanInfomation];
    [self _loadCashCopon];
    
}
- (void)_loadSalemanInfomation {

    [[MTicketService sharedClient]getMerchantInfoWithSalemanUserID:self.ticketModel.salemanUserID completion:^(MSalemanModel *model){
        
        self.salemanNameLabel.text = model.companyName;
        self.salemanAddressLabel.text = model.companyAddress;
        self.salemanPhoneLabel.text = model.phone;
        self.noticeLabel.text = model.getTicketMemo;
        
    } failure:^(NSString *error){
        
        
    }];
}
- (void)_loadCashCopon{

    [[MTicketService sharedClient] getCashCoponWithCodeID:self.ticketModel.codeID completion:^(NSMutableArray *result){
    
        self.friendCountLabel.text = [(NSNumber *)result[1] stringValue];
        
        if ([(NSNumber *)result[1] intValue] >= 3000) {
            self.fullLevelLabel.text = @"(已爆级)";
        }
        int level = 0;
        for (int i = ((NSArray*)result[2]).count-1; i>=0; i--) {
            if ([(NSNumber *)result[2][i][@"Money"] intValue] == [(NSNumber *)result[0] intValue]) {
                level = 15 - i;
                break;
            }
        }
        self.rateLabel.text = [NSString stringWithFormat:@"%d",level];
        self.progressBar.progress = level/15.0f;
        
        self.nowPriceLabel.frame = CGRectMake(self.nowPriceLabel.frame.origin.x+self.progressBar.frame.size.width*(level/15.0f), self.nowPriceLabel.frame.origin.y, self.nowPriceLabel.frame.size.width, self.nowPriceLabel.frame.size.height);
        self.nowPriceLabel.text = [NSString stringWithFormat:@"%@.00",[result[0] stringValue]];
    } failure:^(NSString *error){}];
}
- (void)_loadFeeCash {

    self.feeLabel.text = [NSString stringWithFormat:@"%@.00", self.ticketModel.fee];
    self.originalCashLabel.text = [NSString stringWithFormat:@"%@.00",self.ticketModel.originalMoney];
    self.currentCashLabel.text = [NSString stringWithFormat:@"%@.00",self.ticketModel.money];
    
}
- (void)_initButton{

    self.moneyView.backgroundColor = WHITESMOLKE;
    [self.merchantDetailBtn.layer setCornerRadius:3.0];
    [self.merchantDetailBtn.layer setMasksToBounds:YES];
    [self.merchantDetailBtn.layer setBorderColor:[UIColor blackColor].CGColor];
    [self.merchantDetailBtn.layer setBorderWidth:1.0];
    [self.specailBtn.layer setCornerRadius:3.0];
    [self.specailBtn.layer setMasksToBounds:YES];
    [self.specailBtn.layer setBorderColor:[UIColor blackColor].CGColor];
    [self.specailBtn.layer setBorderWidth:1.0];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)backAction:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)merchantDetailAction:(UIButton *)sender {
}
- (IBAction)specailAction:(UIButton *)sender {
}
@end
